import { createRouter, createWebHistory } from 'vue-router';
import { useUserStore } from '@/stores/user';
import message from '@/utils/message';
// 基本架构页
import Layout from '@/views/Layout.vue';
import Logins from '@/views/Logins.vue';
import ModalPage from '@/views/ModalPage.vue';
// 一级菜单页
import MainPage from '@/views/pages/MainPage.vue';
import UserPage from '@/views/pages/UserPage.vue';
import DataPage from '@/views/pages/DataPage.vue';
import EditPage from '@/views/pages/EditPage.vue';
// 二级弹出页
import SettingPage from '@/views/pages/SettingPage.vue';
import IncomeInput from '@/views/pages/IncomeInput.vue';
import ExpenseInput from '@/views/pages/ExpenseInput.vue';
import FamilyPage from '@/views/pages/FamilyPage.vue';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      name: "登录注册",
      path: '/login',
      component: Logins
    },
    {
      path: '/home',
      component: Layout,
      redirect: '/home/main',
      children: [
        {
          name: "iAccount",
          path: 'main',
          component: MainPage
        },
        {
          name: "用户中心",
          path: 'user',
          component: UserPage
        },
        {
          name: "统计中心",
          path: 'data',
          component: DataPage
        },
        {
          name: "个人记账",
          path: 'edit',
          component: EditPage
        }
      ]
    },
    {
      name: "弹出页面",
      path: '/p',
      component: ModalPage,
      children: [
        {
          name: "系统设置",
          path: 'settings',
          component: SettingPage
        },
        {
          name: "编辑收入",
          path: 'income',
          component: IncomeInput
        },
        {
          name: "编辑支出",
          path: 'expense',
          component: ExpenseInput
        },
        {
          name: "家庭成员管理",
          path: 'family',
          component: FamilyPage
        },
      ]
    }
  ]
});

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  const useUser = useUserStore();
  if(to.path === '/login' || to.path === '/') {
    useUser.exitLogin();
    next();
  } else if(useUser.loginStatus) {
    next();
  } else {
    message.warning('请登录后访问', 1500);
    next('/');
  }
});

export default router;
